<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  ~ Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
  --%>

<%--
  Created by IntelliJ IDEA.
  User: inkux
  Date: 2018/5/25
  Time: 14:58
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<html>
<head>
	<title>Modify Department</title>
	<%@include file="../resources/js_css_includes.jsp" %>
	<script>
		$(document).ready(function () {
			$("#dept_name").change(function () {
				$.ajax({
					url: "ajax/check_department_name",
					data: {
						except: "${dept.name}",
						groupName: $("#dept_name").val()
					},
					type: "post",
					dataType: "json",
					success: function (data) {
						var exists = data.exists;
						if (exists) {
							$("#dept_name").css("border", "1px solid red");
						} else {
							$("#dept_name").css("border", "1px solid green");
						}
					},

					error: function (jqHXR, textStatus, error) {
					}
				});
			});
			$("#modify_form").submit(function () {
				$("#modify_form").ajaxSubmit({
					url: "modify_department",
					target: null,
					type: "post",
					dataType: "json",
					success: function (data) {
						if (data.status == "success") {
							showMessagePopup("Success", "Department successfully updated.",
								function () {
									window.top.close();
								});
						} else {
							showErrorPopup("Error", data.errorMessage);
						}
					},

					error: function (jqHXR, textStatus, error) {
						showErrorPopup("Error", "Could not connect to server");
					}
				});
				return false;
			});
		});
	</script>
</head>
<body>
<%@include file="../resources/navigation_header.jsp"%>
<%@include file="../resources/jsp_body_preludes.jsp" %>
<div class="table_div_forall">
<c:choose>
	<c:when test="${dept != null}">
		<form id="modify_form" action="" method="post">
			<table>
			<tr>
				<td><label for="dept_id">Dept. ID</label></td>
				<td><input class="input" readonly id="dept_id" name="id" type="text"
				           value="${dept.id}"></td>
			</tr>
			<tr>
				<td><label for="dept_name">Name</label></td>
				<td><input class="input" id="dept_name" name="name" type="text"
				           value="${dept.name}"></td>
			</tr>
			<tr>
				<td><label for="dept_desc">Description</label></td>
				<td><input class="input" id="dept_desc" name="description" type="text"
				           value="${dept.description}"></td>
			</tr>
			<tr>
				<td><input class="btn_color2" type="submit" id="submit_button" value="Update"/></td>
			</tr>
			</table>

		</form>
	</c:when>
	<c:otherwise>
		<div style="color: red;">${errorMessage}</div>
	</c:otherwise>
</c:choose>
</div>
</body>
</html>
